<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      
      <el-form-item label="申请人姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:approval:edit']"
        >审批</el-button>
      </el-col>
    
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请人工号" align="center" prop="userId" />
      <el-table-column label="申请人姓名" align="center" prop="userName" />
      <el-table-column label="请假开始时间" align="center" prop="leaveStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请假结束时间" align="center" prop="leaveEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请假备注" align="center" prop="leaveRemarks" />
      <el-table-column label="审批状态" align="center" prop="approvalResults">
        <template slot-scope="scope">
          <span class="el-tag el-tag--info el-tag--medium el-tag--light" v-if="scope.row.approvalResults === '0'"> 待审批 </span>
          <span class="el-tag el-tag--success el-tag--medium el-tag--light" v-if="scope.row.approvalResults === '1'">审批通过</span>
          <span class="el-tag el-tag--medium el-tag--light" v-if="scope.row.approvalResults === '3'"> 驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="请假状态" align="center" prop="leaveStatus" />
      <el-table-column label="审批意见" align="center" prop="approvalRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:approval:edit']"
          >审批</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改请假信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="请假开始时间" prop="leaveStartTime">
          <el-date-picker clearable
            v-model="form.leaveStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择请假开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请假结束时间" prop="leaveEndTime">
          <el-date-picker clearable
            v-model="form.leaveEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择请假结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请假备注" prop="leaveRemarks">
          <el-input v-model="form.leaveRemarks" placeholder="请输入请假备注" />
        </el-form-item>
        <el-form-item label="审批意见" prop="approvalRemark">
          <el-input v-model="form.approvalRemark" placeholder="请输入审批意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(1)">通 过</el-button>
        <el-button @click="cancel">驳 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInformation, getInformation, delInformation, addInformation, updateInformation } from "@/api/system/approval";

export default {
  name: "Information",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假信息表格数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        leaveStartTime: null,
        leaveEndTime: null,
        leaveRemarks: null,
        approvalResults: null,
        leaveStatus: null,
        approvalRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请假信息列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lineId: null,
        userId: null,
        userName: null,
        leaveStartTime: null,
        leaveEndTime: null,
        leaveRemarks: null,
        approvalResults: null,
        leaveStatus: null,
        approvalRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lineId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lineId = row.lineId || this.ids
      getInformation(lineId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改请假信息";
      });
    },
    /** 提交按钮 */
    submitForm(t) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lineId != null) {
            if(t==1){ 
                 this.form.approvalResults="1";
              }
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInformation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lineIds = row.lineId || this.ids;
      this.$modal.confirm('是否确认删除请假信息？').then(function() {
        return delInformation(lineIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
